/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.business.controller.bond;

import com.shop.cereshop.business.page.bond.ShopRechargeVO;
import com.shop.cereshop.business.param.bond.ShopRechargeDTO;
import com.shop.cereshop.business.service.bond.CereShopBondRechargeService;
import com.shop.cereshop.business.utils.ContextUtil;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.bond.CereShopBondRecharge;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 店铺保证金充值记录模块
 */
@Slf4j
@RestController
@RequestMapping("/bond/recharge")
@Api(value = "店铺保证金充值模块", tags = "店铺保证金充值模块")
@RequiredArgsConstructor
public class CereShopBondRechargeController {

    private final CereShopBondRechargeService cereShopBondRechargeService;

    /**
     * 店铺保证金创建充值订单
     */
    @PostMapping("/create")
    @ApiOperation(value = "店铺保证金创建充值订单")
    public Result<ShopRechargeVO> createRechargeOrder(@Valid @RequestBody ShopRechargeDTO param) throws CoBusinessException {
        try {
            //获取当前登录店铺
            Long shopId = ContextUtil.getShopId();
            if(shopId == null) {
                return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
            }

            //设置店铺id
            param.setShopId(shopId);
            ShopRechargeVO result = cereShopBondRechargeService.createRechargeOrder(param);
            return Result.success(result);
        } catch (Exception e) {
            log.error("创建保证金充值订单失败，金额:{}", param.getAmount(), e);
            return Result.failed(CoReturnFormat.RECHARGE_CREATE_FAIL);
        }
    }

    /**
     * 获取店铺保证金充值记录列表
     */
    @ApiOperation(value = "获取店铺保证金充值记录列表")
    @GetMapping("/list")
    public Result<List<CereShopBondRecharge>> getRechargeList(@RequestParam(defaultValue = "1") @ApiParam(value = "当前页码", required = true) Integer page,
                                                              @RequestParam(defaultValue = "10") @ApiParam(value = "每页数量", required = true) Integer pageSize) throws CoBusinessException {
        Long shopId = ContextUtil.getShopId();
        if(shopId == null) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        List<CereShopBondRecharge> result = cereShopBondRechargeService.getRechargeList(shopId, page, pageSize);
        return Result.success(result);
    }

    @ApiOperation("根据充值订单号查询充值详情")
    @GetMapping("/detail")
    public Result<CereShopBondRecharge> getRechargeDetail(@RequestParam("rechargeNo") @ApiParam(value = "充值单号", required = true) String rechargeNo) {
        return Result.success(cereShopBondRechargeService.getRechargeDetail(rechargeNo));
    }

    @ApiOperation("根据充值ID查询充值详情")
    @GetMapping("/detailById")
    public Result<CereShopBondRecharge> getDetailById(@RequestParam(value = "rechargeId") @ApiParam(value = "充值ID", required = true) Long rechargeId) {
        return Result.success(cereShopBondRechargeService.getDetailById(rechargeId));
    }

    @ApiOperation("取消充值订单")
    @PostMapping("/cancel")
    public Result<Boolean> cancelRechargeOrder(@RequestParam String rechargeNo) throws CoBusinessException {
        try {
            //获取当前店铺id
            Long shopId = ContextUtil.getShopId();
            if (shopId == null) {
                return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
            }
            Boolean result = cereShopBondRechargeService.cancelRechargeOrder(rechargeNo, shopId);
            if (result) {
                return Result.success(result);
            } else  {
                return Result.failed(CoReturnFormat.RECHARGE_CANCEL_FAIL);
            }
        } catch (Exception e) {
            log.error("取消充值订单失败, 单号:{}", rechargeNo, e);
            return Result.failed(CoReturnFormat.RECHARGE_CANCEL_FAIL);
        }
    }

    @ApiOperation("处理充值成功回调")
    @PostMapping("/callback/success")
    public Result<Boolean> handleRechargeSuccess(@RequestParam @ApiParam(value = "充值单号", required = true) String rechargeNo,
                                                @RequestParam @ApiParam(value = "第三方订单号", required = true) String thirdPartyOrderNo){
        try {
            boolean success = cereShopBondRechargeService.handleRechargeSuccess(rechargeNo, thirdPartyOrderNo);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed(CoReturnFormat.RECHARGE_CALLBACK_FAIL, false);
            }
        } catch (Exception e) {
            log.error("处理充值成功回调失败, 充值单号:{}, 第三方订单号:{}", rechargeNo, thirdPartyOrderNo, e);
            return Result.failed(CoReturnFormat.RECHARGE_CALLBACK_FAIL, false);
        }
    }

    @ApiOperation("处理充值失败回调")
    @PostMapping("/callback/fail")
    public Result<Boolean> handleRechargeFail(@RequestParam @ApiParam(value = "充值单号", required = true) String rechargeNo,
                                             @RequestParam @ApiParam(value = "失败原因", required = true) String failReason) {
        try {
            boolean result = cereShopBondRechargeService.handleRechargeFail(rechargeNo, failReason);
            if (result) {
                return Result.success(true);
            } else {
                return Result.failed(CoReturnFormat.RECHARGE_CALLBACK_FAIL, false);
            }
        } catch (Exception e) {
            log.error("处理充值失败回调失败, 充值单号:{}", rechargeNo, e);
            return Result.failed(CoReturnFormat.RECHARGE_CALLBACK_FAIL, false);
        }
    }
}